<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-12
 * Time: 1:01
 */

namespace app\common\logic;
use app\common\model\Module;
use think\Model;
class Logic extends Model
{
//    use \app\common\traits\Curd;
    
    //sql都要用到的参数
    protected $modelClass = null;
    protected $column = null;//这里有的参数，如果ajax有传的话，就会被放到condition里面
    protected $condition = null;
    protected $field = null;
    protected $order = null;
    protected $with = null;
    protected $paginate = null;
    protected $size = 10;
    protected $id = null;


    
    /**
     * 保存（新增）
     */
    public function add($param){
        
        $modelClass = $this->modelClass;
        $model = new $modelClass();
        
        $rt = $model->allowField(true)->save($param);
    
        
        if(isset($model->id)){
            return $model->id;
        }
        return $rt?true:false;
        
    }
    
    /**
     * 保存（新增）
     */
    public function adds($param){
        
       
        $modelClass = $this->modelClass;
        $model = new $modelClass();
       
        $ids = $model->allowField(true)->saveAll($param);
       
        return $ids?$ids:false;
    }

    
    /**
     * 修改
     */
    public function edit($id,$param){
        
        $modelClass = $this->modelClass;
        $this->condition['id']=$id;
        
        $m = new $modelClass();
        $rt = $m ->allowField(true)->save($param,$this->condition);

        if($rt||$rt==0){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * 删除 - 不是真的删除，而是修改状态而已。
     * @param $id
     * @return string
     */
    public function del($id)
    {
        
        $model = $this->findModel($id);
        $rt = $model->delete();
        
        if ($rt) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * find model
     * @param $id
     * @return mixed
     */
    public function findModel($id=null){
        
        $modelClass = $this->modelClass;
        $condition['id']=$id?$id:$this->id;
        
        //关联
        if($this->with){
            $query = $modelClass::with($this->with)->where($condition);
        }else{
            $query = $modelClass::where($condition);
        }
        

        // 筛选字段
        if($this->field){
            $query = $query->field($this->field);
        }
        
        $model = $query->find();
        
        //自动加点击数
        if(isset($model['hits'])){
            $model->hits +=1;
            $model->isUpdate()->save();
        }
        
        if(!$model){
            error("你没权限操作或该记录已经被删除！");
        }
        return $model;
    }
    
    /**
     * find list
     * @return mixed
     */
    public function prepareDataProvider(){
    
       
        $list = null;
        
        $modelClass = $this->modelClass;
       
        $query = null;
        // 条件
        if($this->condition){
            $query = $modelClass::where($this->condition);
        }
        
        // 筛选字段
        if($this->field){
            if($query){
                $query = $query->field($this->field);
            }else{
                $query = $modelClass::field($this->field);
            }
        }
        
        
        // 排序
        if($this->order){
            if($query){
                $query = $query->order($this->order);
            }else{
                $query = $modelClass::order($this->order);
            }
        }
        
        // 关联预载入
        if($this->with){
            if($query){
                $query = $query->with($this->with);
            }else{
                $query = $modelClass::with($this->with);
            }
        }
        
       

        // 分页查询
        if($this->paginate){
            if($query){
                $list = $query->paginate($this->size);
            }else{
                $list = $modelClass::paginate($this->size);
            }
            
        }else{
            //如果数字为null，则返回所有的
            if(!$this->size){
                
                if($query){
                    $list = $query->select();
                }else{
                    $list = $modelClass::all();
                }
                
            }else{
                
                if($query){
                    $list = $query->limit($this->size)->select();
                }else{
                    $list = $modelClass::limit($this->size)->select();
                }
            }
            
        }
        
        return $list;
    }
    
    
    public function setSql($params){
        
        foreach ($params as $key=>$value){
            
            //注入condition参数
            if(strpos($this->column,$key)>-1){
                $this->condition[$key] = $value;
            }
            
            //注入其他条件
            if(strpos('id,field,order,with,limit,size,paginate,page',$key)>-1){
                $this->$key = $value;
            }
        }
        
    }
    
    
    
    public function getList($sqlObj){
        
        $this->setSql($sqlObj);
        
        $lists = $this->prepareDataProvider();
        return $lists;
        
    }
    
    public function  setValue($key,$value){
        $this->$key = $value;
        
    }
    
  
}